package com.yuandian.management.controller;

import com.yuandian.api.management.po.OptionParams;
import com.yuandian.api.management.vo.OptionVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.enums.DataTypeEnum;
import com.yuandian.management.service.IOptionService;
import com.yuandian.management.vo.BusinessServerResourceParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 说明：选项controller
 * @Author zfsn
 * @Date 2023/2/6 11:22
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/option")
@Api(tags = "下拉选项接口")
public class OptionController {

	private final IOptionService optionService;

	/**
	 * 查询系统选项
	 * @param params 选项请求参数
	 * @return 统一响应
	 */
	@ApiOperation(value = "系统选项", notes = "查询系统选项，默认查询所有系统选项，也支持通过传入参数过滤部分系统选项")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "isStatus", value = "是否监控中", paramType = "query", required = false, dataTypeClass = Integer.class)
	})
	@GetMapping("/system")
	public R<List<OptionVo>> system(@ApiIgnore OptionParams params) {
		return R.ok(optionService.system(params));
	}

	/**
	 * 查询业务选项
	 * @param params 选项请求参数
	 * @return 统一响应
	 */
	@ApiOperation(value = "业务选项", notes = "查询业务选项，默认查询所有业务选项，也支持通过传入参数过滤部分业务选项")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "isStatus", value = "是否监控中", paramType = "query", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "query", required = true, dataTypeClass = Integer.class)
	})
	@GetMapping("/business")
	public R<List<OptionVo>> business(@ApiIgnore OptionParams params) {
		return R.ok(optionService.business(params));
	}

	/**
	 * 查询步骤选项
	 * @param params 选项请求参数
	 * @return 统一响应
	 */
	@ApiOperation(value = "步骤选项", notes = "查询步骤选项，默认查询所有步骤选项，也支持通过传入参数过滤部分步骤选项")
	@GetMapping("/step")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "isStatus", value = "是否监控中", paramType = "query", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "query", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "businessCode", value = "业务编码", paramType = "query", required = false, dataType = "String"),
			@ApiImplicitParam(name = "businessCodes", value = "业务编码(多选)", paramType = "query", required = false, dataType = "String")
	})
	public R<List<OptionVo>> step(@ApiIgnore OptionParams params) {
		return R.ok(optionService.step(params));
	}

	/**
	 * 查询网端选项
	 * @param params 选项请求参数
	 * @return 统一响应
	 */
	@ApiOperation(value = "网端选项", notes = "查询网端选项，默认查询所有网端选项，也支持通过传入参数过滤部分网端选项")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统id", paramType = "query", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "type", value = "与端类型id对应(支持多选)", paramType = "query", required = false, dataTypeClass = Integer.class)
	})
	@GetMapping("/segment")
	public R<List<OptionVo>> segment(@ApiIgnore OptionParams params) {
		return R.ok(optionService.segment(params));
	}


	/**
	 * 告警配置管理-告警模板配置-模板类型
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-告警模板配置-模板类型", notes = "1: 基础告警, 2: 自定义告警")
	@GetMapping("/templateType")
	public R<List<OptionVo>> templateType() {
		return R.ok(optionService.templateType());
	}

	/**
	 * 告警配置管理-告警模板配置-指标单位
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-告警模板配置-指标单位", notes = " ")
	@GetMapping("/indicatorsType")
	public R<List<OptionVo>> indicatorsType() {
		return R.ok(optionService.indicatorsType());
	}

	/**
	 * 告警配置管理-告警模板配置-告警对象
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-告警模板配置-告警对象", notes = " ")
	@GetMapping("/indicatorsObject")
	public R<List<OptionVo>> indicatorsObject() {
		return R.ok(optionService.indicatorsObject());
	}

	/**
	 * 通过系统id查询端类型
	 */
	@ApiOperation(value = "通过系统id查询端类型", notes = "通过系统id查询端类型")
	@GetMapping("/querySegmentTypeBySystemCode/{systemId}")
	public R<List<OptionVo>> getSegmentTypeNameBySegmentId(@PathVariable("systemId") Integer systemId) {
		return R.ok(optionService.getSegmentTypeNameBySegmentId(systemId));
	}

	/**
	 * 告警配置管理-告警模板配置-颗粒度
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-告警模板配置-颗粒度", notes = " ")
	@GetMapping("/indicatorsParticle")
	public R<List<OptionVo>> indicatorsParticle() {
		return R.ok(optionService.indicatorsParticle());
	}

	/**
	 *	告警级别
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-告警级别", notes = " ")
	@GetMapping("/alarmLevel")
	public R<List<OptionVo>> alarmLevel() {
		return R.ok(optionService.alarmLevel());
	}

	/**
	 *	告警模板
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-告警模板", notes = " ")
	@GetMapping("/alarmTemplate")
	public R<List<Map<String, Object>>> alarmTemplate() {
		return R.ok(optionService.alarmTemplate(null));
	}

	/**
	 *	告警模板
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-基础告警模板", notes = " ")
	@GetMapping("/baseAlarmTemplate")
	public R<List<Map<String, Object>>> baseAlarmTemplate() {
		return R.ok(optionService.alarmTemplate(1));
	}


	/**
	 *	自定义告警模板
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-自定义告警模板", notes = " ")
	@GetMapping("/customAlarmTemplate")
	public R<List<Map<String, Object>>> customAlarmTemplate() {
		return R.ok(optionService.alarmTemplate(2));
	}


	/**
	 *	基础告警指标
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警指标(包含基础告警和自定义告警)", notes = " ")
	@GetMapping("/alarmIndicators")
	public R<List<OptionVo>> AlarmIndicators(String category) {
		return R.ok(optionService.alarmIndicators(null, category));
	}

	/**
	 *	基础告警指标
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-基础告警指标", notes = " ")
	@GetMapping("/baseAlarmIndicators")
	public R<List<OptionVo>> baseAlarmIndicators(String category) {
		return R.ok(optionService.alarmIndicators(1, category));
	}

	/**
	 *	自定义告警指标
	 * @return 统一响应
	 */
	@ApiOperation(value = "告警配置管理-自定义告警指标", notes = " ")
	@GetMapping("/customAlarmIndicators")
	public R<List<OptionVo>> customAlarmIndicators(String category) {
		return R.ok(optionService.alarmIndicators(2, category));
	}


	/**
	 *	数据中心
	 * @return 统一响应
	 */
	@ApiOperation(value = "数据中心", notes = " ")
	@GetMapping("/dataCenter")
	public R<List<OptionVo>> dataCenter() {
		return R.ok(optionService.dataCenter());
	}

	/**
	 *	数据中心下的地市
	 * @return 统一响应
	 */
	@ApiOperation(value = "数据中心下的地市", notes = " ")
	@GetMapping(value = {"/getCityArea/{dataCenterId}", "/getCityArea"})
	@ApiImplicitParams({
			@ApiImplicitParam(name = "dataCenterId", value = "数据中心", paramType = "path", required = false, dataTypeClass = Integer.class)
	})
	public R<List<OptionVo>> getCityAreaByDataCenterId(@PathVariable(value = "dataCenterId", required = false) Integer dataCenterId) {
		return R.ok(optionService.getCityAreaByDataCenterId(dataCenterId));
	}

	/**
	 *	查询菜单映射关系
	 * @return 统一响应
	 */
	@ApiIgnore
	@GetMapping("/buttonMap")
	public R<List<OptionVo>> getButtonMap() {
		return R.ok(optionService.getButtonMap());
	}

	/**
	 *	查询城市地市(区域)
	 * @return 统一响应
	 */
	@ApiOperation(value = "查询城市地市(区域)", notes = " ")
	@GetMapping(value = {"/cityArea/{provinceCode}", "/cityArea"})
	@ApiImplicitParams({
			@ApiImplicitParam(name = "provinceCode", value = "省份编码", paramType = "path", required = false, dataType = "String")
	})
	public R<List<OptionVo>> getCityArea(@ApiIgnore @PathVariable(value = "provinceCode", required = false) String provinceCode) {
		return R.ok(optionService.getCityArea(provinceCode));
	}

	/**
	 *	查询省份
	 * @return 统一响应
	 */
	@ApiOperation(value = "查询省份", notes = "查询省份")
	@GetMapping("/province")
	public R<List<OptionVo>> province() {
		return R.ok(optionService.province());
	}


	/**
	 *	查询告警类型
	 * @return 统一响应
	 */
	@ApiOperation(value = "查询告警类型", notes = "查询告警类型")
	@PostMapping("/queryBaseIndicatorsList")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "category", value = "告警类型", paramType = "body", required = false, dataType = "String")
	})
	public R<List<OptionVo>> queryBaseIndicatorsList(@RequestBody @ApiIgnore OptionParams params) {
		return R.ok(optionService.queryBaseIndicatorsList(params));
	}

	/**
	 *	获取协议名称
	 * @return
	 */
	@ApiOperation(value = "获取协议名称", notes = "获取协议名称")
	@GetMapping("/protocol")
	public R<List<OptionVo>> protocol() {
		return R.ok(optionService.protocol());
	}



	/**
	 *	查询系统所属的域
	 * @return 统一响应
	 */
	@ApiOperation(value = "查询系统所属的域", notes = "查询系统所属的域")
	@PostMapping("/getSystemDomain")
	public R<List<OptionVo>> getSystemDomain() {
		return R.ok(optionService.getSystemDomain());
	}

	/**
	 *	原始数据规则名称
	 * @return 统一响应
	 */
	@ApiOperation(value = "原始数据规则名称", notes = "原始数据规则名称")
	@PostMapping("/queryRuleNameSelect")
	public R<List<OptionVo>> queryRuleNameSelect() {
		return R.ok(optionService.queryRuleNameSelect());
	}


	/**
	 *	指标单位
	 * @return 统一响应
	 */
	@ApiOperation(value = "指标单位", notes = "指标单位")
	@GetMapping("/indicatorUnit")
	public R<List<OptionVo>> indicatorUnit() {
		return R.ok(optionService.indicatorUnit());
	}

	/**
	 * 查询告警对象：
	 * 		数据类型: 1-业务，2-区域，3-步骤，4-接口，5-高频，6-經常，7-低频，8-零散
	 *
	 * @return 通用返回对象
	 */
	@ApiIgnore
	@GetMapping("/queryObservationObject")
	public R<Object> queryObservationObject() {
		List<OptionVo> list = new ArrayList<>();

		for (DataTypeEnum value : DataTypeEnum.values()) {
			if (!Objects.equals(value.getDataType(), DataTypeEnum.businessType.getDataType()) ||
					!Objects.equals(value.getDataType(), DataTypeEnum.areaType.getDataType()) ) {

				OptionVo vo = new OptionVo();
				vo.setLabel(value.getDesc());
				vo.setValue(value.getDataType().toString());
				list.add(vo);
			}
		}

		return R.ok(list);
	}



	/**
	 * 查询专属指标同步网端下拉项
	 * @param params 业务服务资源接口请求对象
	 * @return 通用返回对象
	 */
	@GetMapping("/queryIndicatorSyncSegmentOptions")
	public R queryIndicatorSyncSegmentOptions(BusinessServerResourceParams params) {
		return optionService.queryIndicatorSyncSegmentOptions(params);
	}

	/**
	 * 查询专属指标同步系统下拉项
	 * @param params 业务服务资源接口请求对象
	 * @return 通用返回对象
	 */
	@GetMapping("/queryIndicatorSyncSystemOptions")
	public R queryIndicatorSyncSystemOptions(BusinessServerResourceParams params) {
		return optionService.queryIndicatorSyncSystemOptions(params);
	}

	/**
	 * 查询专属指标同步业务下拉项
	 * @param params 业务服务资源接口请求对象
	 * @return 通用返回对象
	 */
	@GetMapping("/queryIndicatorSyncBusinessOptions")
	public R queryIndicatorSyncBusinessOptions(BusinessServerResourceParams params) {
		return optionService.queryIndicatorSyncBusinessOptions(params);
	}


}
